<?php
namespace app\index\controller;
use app\index\model\CrmCustomer;
use think\Db;
use think\Request;
use think\Controller;

use excel\Excels as excels;
use app\index\model\GoodsPhone;
use app\index\model\GoodsNext;
use polyv\Blw;
use think\Exception;
use app\index\model\Goods;


class Test extends Base
{
    // 定时任务访问链接模式完成客户数据统计
    public function runCustomerManage()
    {
        // https://api.xuexiang.com.cn/index/test/runCustomerManage/token/7df32de82d0a5e4d2599427eddcb4cee37db2bdb
        self::customerManageInsert();
    }

    // 定时任务访问链接模式完成客户数据统计
    public function runCustomer()
    {
        // https://api.xuexiang.com.cn/index/test/runCustomer/token/7df32de82d0a5e4d2599427eddcb4cee37db2bdb
        self::yaoyueGonghaiAdd();

        self::publicyingqiRecover();

        self::sourceRecover();
    }

    // 定时任务记录昨天客户数据分配记录
    public function customerManageInsert()
    {
        // https://api.xuexiang.com.cn/index/test/customerManageInsert/token/7df32de82d0a5e4d2599427eddcb4cee37db2bdb
        //今天记录昨天客户转移情况
        $starttime = date('Y-m-d',strtotime("-1 day"))." 00:00:00";
        $endtime = date('Y-m-d',strtotime("-1 day"))." 23:59:59";
        $where['date'] = array(array('egt',strtotime($starttime)),array('elt',strtotime($endtime)));
        $where['zhuanyi_date'] = array('neq','null');

        $customerList = db('crm_customer')->where($where)->column('client_id');
        // var_dump($customerList);exit;
        foreach ($customerList as $key => $value) {
            $this->customerManageAdd($value,1,0);
        }
        echo "记录昨天数据分配记录完成 ";
    }

    // 定时任务邀约3天后未加微信的客户，放入邀约公海
    public function yaoyueGonghaiAdd()
    {
        // https://api.xuexiang.com.cn/index/test/yaoyueGonghaiAdd/token/7df32de82d0a5e4d2599427eddcb4cee37db2bdb
        //今天记录昨天客户转移情况
        $starttime = date('Y-m-d',strtotime("-7 day"))." 00:00:00";
        $endtime = date('Y-m-d',strtotime("-7 day"))." 23:59:59";
        $where['zhuanyi_date'] = array(array('egt',strtotime($starttime)),array('elt',strtotime($endtime)));
        $customerList = db('crm_customer')->field('client_id,yaoyue_status')->where($where)->select();
        // var_dump($customerList);exit;
        foreach ($customerList as $key => $value) {
            if ($value['yaoyue_status'] != 1) {
                //进入邀约公海
                db('crm_customer')->where('client_id',$value['client_id'])->update(['yaoyue_manage_id'=>null,'yaoyue_gonghai'=>1]);
            }
        }
        echo "放入邀约公海完成 ";
    }

    // 公开课营期修复
    public function publicyingqiRecover()
    {
        // https://api.xuexiang.com.cn/index/test/publicyingqiRecover/token/7df32de82d0a5e4d2599427eddcb4cee37db2bdb
        $where['public_yingqi'] = ['neq','null'];
        $where1['public_yingqi'] = ['neq','0'];
        $where2['public_yingqi_course1'] = null;
        //近60天
        $starttime = date('Y-m-d',strtotime("-60 day"))." 00:00:00";
        $endtime = date('Y-m-d',mktime(23,59,59,date("m"),date("t"),date("Y")))." 23:59:59";
        $where3['date'] = array(array('egt',strtotime($starttime)),array('elt',strtotime($endtime)));
        $list = db('crm_customer')->field('client_id,public_yingqi,public_yingqi_course1')->where($where)->where($where1)->where($where2)->where($where3)->select();
        // var_dump($list);exit;

        foreach ($list as $key => $value) {
            $next_course = db('goods_next')->field('id')->where('goods_id',$value['public_yingqi'])->limit(0,2)->order('id ASC')->select();
            // var_dump($next_course);exit;
            if (isset($next_course[0]['id']) && $next_course[0]['id'] != null) {
                $customer_datas['public_yingqi'] = $value['public_yingqi'];
                $customer_datas['public_yingqi_course1'] = $next_course[0]['id'];
                $customer_datas['public_yingqi_course2'] = $next_course[1]['id'];
                db('crm_customer')->where('client_id',$value['client_id'])->update($customer_datas);
                $this->customerManageAdd($value['client_id']);
            }
        }
        echo "公开课营期修复完成 ";
    }

    public function sourceRecover()
    {
        // https://api.xuexiang.com.cn/index/test/sourceRecover/token/36b80acac6c085d3c445f931d0ed9c149d01b99e
        // $where['date'] = array(array('egt',strtotime("2023-05-29 00:00:00")),array('elt',strtotime("2023-05-29 23:59:59")));
        $list1 = db('crm_customer')->field(true)->limit(5000)->page(1)->select();
        apiReturn(200,'',$list1);
        $where['source_type'] = ['like','552&%'];

        $list = db('crm_customer')->where($where)->column('client_id');
        // var_dump($list);exit;
        foreach ($list as $key => $value) {
            db('crm_customer')->where('client_id',$value)->update(['source'=>'0元公开课','source_type'=>552]);
        }

        $where1['source_type'] = ['like','290&%'];

        $list1 = db('crm_customer')->where($where1)->column('client_id');
        // var_dump($list);exit;
        foreach ($list1 as $key => $value) {
            db('crm_customer')->where('client_id',$value)->update(['source'=>'图书0元','source_type'=>290]);
        }


        echo "渠道乱码链接修复完成 ";

    }

    // 缩略图测试
    public function test()
    {
        $data = input();
        $url = "https://xuexiang.oss-cn-hangzhou.aliyuncs.com/1677836940.png";
        $url = $this->thumbPic($url, 200, 112);
        var_dump($url);exit;
    }

    public function exceltest()
    {
        $data = input();
        $url = "https://xuexiang.oss-cn-hangzhou.aliyuncs.com/excel.xlsx";
        $array = excels::readExcelFile($url);
        var_dump($array);exit;
    }

    public function testaaa()
    {
        $orderStatus = db('crm_order')->group('order_status')->field('id,max(id) as ids,order_status')->select();
        var_dump($orderStatus);
    }

    public function yaoyueGonghaiList()
    {
        $where['yaoyue_gonghai'] = 1;
        $customerList = db('crm_customer')->field('client_id,yaoyue_status')->where($where)->select();
        var_dump($customerList);exit;
    }

    public function toYaoyueGonghai()
    {
        $where['client_id'] = ['in','165678,165671,165392,165356,164558,164488,164447,164091,163904,163601,163526,163412,163223,163072,163007,163005,162832,162289,162230,162173,162032,161318,161439,161803,160998,161402,161018,161010,161307,160717,160563,160556,160546,160484,160387,160243,160125,160040,160028,159574,159024,158973,158537,158480,158220,157645,157495,157337,156274,156403,156273,154712,154743,154762,154824,154840,154870,154970,154976,154190,154448,154361,154057,153529,154000,153821,152881,153253,153242,153136,153096,153182,153111,153157,153208,153142,151875,152217,152544,152439,151616,151596,150426,150424,150375,150434,150406,150379,150269,150436,148831,147779,147886,147932,145764,140964,141064,139817,140116,138078,138303'];
        $list = db('crm_customer')->where($where)->column('client_id');
        var_dump($list);exit;
        foreach ($list as $key => $value) {
            db('crm_customer')->where('client_id',$value)->update(['yaoyue_manage_id'=>null,'yaoyue_gonghai'=>1]);
        }
        echo "运行完成";
    }

    public function insertToRegion()
    {
        // http://192.168.0.22/index/index/insertToRegion/token/945e2d93569fa0cade0f307f5c0d545901e5a5c2

        // $province = db('province')->select();

        // foreach ($province as $key => $value) {
        //     $datas = array('region_id'=>$value['id'],'parent_id'=>0,'region_name'=>$value['name'],'region_type'=>1);
        //     db('crm_region1')->insert($datas);
        // }

        // $city = db('city')->select();

        // foreach ($city as $key => $value) {
        //     $datas = array('region_id'=>$value['id'],'parent_id'=>$value['pid'],'region_name'=>$value['name'],'region_type'=>2);
        //     db('crm_region1')->insert($datas);
        // }

        // $area = db('area')->select();

        // foreach ($area as $key => $value) {
        //     $datas = array('region_id'=>$value['id'],'parent_id'=>$value['pid'],'region_name'=>$value['name'],'region_type'=>3);
        //     db('crm_region1')->insert($datas);
        // }

        // $street = db('street')->select();

        // foreach ($street as $key => $value) {
        //     $datas = array('region_id'=>$value['id'],'parent_id'=>$value['pid'],'region_name'=>$value['name'],'region_type'=>4);
        //     db('crm_region1')->insert($datas);
        // }
        echo "运行完成";
    }

    public function test111()
    {
        // $where['diqu'] = array('like','%深圳%');
        // $where['date'] = array(array('egt',strtotime('2023-05-11 00:00:00')),array('elt',strtotime('2023-05-11 23:59:59')));
        // $where['type'] = 16;

        // $list = db('cooperation')->where($where)->column('phone');

        // $where1['phone'] = ['in',implode(',', $list)];

        // $list1 = db('crm_customer')->field('client_id,phone,name,grade,yaoyue_status,is_refund,public_paytype,date')->where($where1)->select();


        // $is_refund=[0=>'',1=>'已退款'];
        // $public_paytype=[0=>'',1=>'直播间下单未付款',2=>'直播间下单',3=>'追单成功'];
        


        // $array[0] = array('客户ID','客户姓名','客户电话','年级','是否退款','报名状态','创建时间');
        // $status=[0=>'',1=>'未完成',2=>'已完成'];
        // foreach ($list1 as $key => $value) {
        //     $array[] = array($value['client_id'],$value['name'],$value['phone'],$value['grade'],$is_refund[$value['is_refund']],$public_paytype[$value['public_paytype']],date("Y-m-d H:i:s",$value['date']));
        // }
        // excels::index($array,'5.11深圳列表 '.date("Y-m-d h.i.s",time()),'','C',0,12,30,25,30);
        
    }

    public function test222()
    {
        $list = [
            ['id'=>'1','address'=>'111'],
            ['id'=>'2','address'=>'222'],
            ['id'=>'3','address'=>'333'],
            ['id'=>'4','address'=>'444']
        ];
        $customer = model('CrmCustomer');
        $customer->saveAll($list);
        echo "运行完成";
    }

    public function test333()
    {
        // http://192.168.0.22/index/index/test333/token/36b80acac6c085d3c445f931d0ed9c149d01b99e
        // https://api.xuexiang.com.cn/index/index/test333/token/36b80acac6c085d3c445f931d0ed9c149d01b99e
        $where['province'] = ['neq','null'];
        $list = db('crm_customer')->field('id,client_id,province,city,area')->where($where)->select();
        var_dump($list);exit;

        foreach ($list as $key => $value) {
            // var_dump($value);exit;
            if (!empty($value['province'])) {
                $province_name = db('crm_region1')->where('region_id',$value['province'])->value('region_name');
                $province_region_id = db('crm_region')->where(['region_name'=>['like','%'.$province_name.'%']])->value('region_id');
                if (!empty($province_region_id)) {
                    $datas['province'] = $province_region_id;
                }
            }
            if (!empty($value['city'])) {
                $city_name = db('crm_region1')->where('region_id',$value['city'])->value('region_name');
                $city_region_id = db('crm_region')->where(['region_name'=>['like','%'.$city_name.'%']])->value('region_id');
                if (!empty($city_region_id)) {
                    $datas['city'] = $city_region_id;
                }
            }
            if (!empty($value['area'])) {
                $area_name = db('crm_region1')->where('region_id',$value['area'])->value('region_name');
                $area_region_id = db('crm_region')->where(['region_name'=>['like','%'.$area_name.'%']])->value('region_id');
                if (!empty($area_region_id)) {
                    $datas['area'] = $area_region_id;
                }
            }
            db('crm_customer')->where('id',$value['id'])->update($datas);
            // var_dump($datas);exit;
        }
        echo "运行完成";
    }

    public function test555()
    {
        // https://api.xuexiang.com.cn/index/index/test555/token/36b80acac6c085d3c445f931d0ed9c149d01b99e
        $where['manage_id'] = '1999';
        $where['date'] = array(array('egt',strtotime("2023-04-01 00:00:00")),array('elt',strtotime("2023-04-30 23:59:59")));
        $where['source'] = ['neq','39元公开课未付款'];
        $list1 = db('crm_customer')->where($where)->select();

        $where1['manage_id'] = '1999';
        $where1['date'] = array(array('egt',strtotime("2023-04-01 00:00:00")),array('elt',strtotime("2023-04-30 23:59:59")));
        $where1['source'] = '39元公开课未付款';
        $where1['become_zhifu'] = 1;
        $list2 = db('crm_customer')->where($where1)->select();

        $list = array_merge($list1, $list2);

        // var_dump(count($list));exit;
        $array[0] = array('客户ID','客户姓名','客户电话','年级','是否上课','是否退款','报名状态','创建时间');
        $is_refund=[0=>'',1=>'已退款'];
        $public_paytype=[0=>'',1=>'直播间下单未付款',2=>'直播间下单',3=>'追单成功'];
        foreach ($list as $key => $value) {
            if (empty($value['public_yingqi'])) {
                $sahngke = '';
            }else{
                $sahngke = '已上课';
            }
            $array[] = array($value['client_id'],$value['name'],$value['phone'],$value['grade'],$sahngke,$is_refund[$value['is_refund']],$public_paytype[$value['public_paytype']],date("Y-m-d H:i:s",$value['date']));
        }
        excels::index($array,'客户列表 '.date("Y-m-d h.i.s",time()),'','C',0,12,30,25,30);

    }

    public function test777()
    {
        // https://api.xuexiang.com.cn/index/index/test777/token/36b80acac6c085d3c445f931d0ed9c149d01b99e
        $where['date'] = array(array('egt',strtotime("2023-05-30 00:00:00")),array('elt',strtotime("2023-05-31 23:59:59")));
        $where['source_type'] = ['in','301,302,303,501,502,503,601,602,603,901,902,903'];
        $where['source'] = '39元公开课未付款';

        $list = db('crm_customer')->where($where)->column('client_id');
        var_dump($list);exit;
        foreach ($list as $key => $value) {
            db('crm_customer')->where('client_id',$value)->update(['source'=>'3元公开课未付款']);
        }
    }

    public function test888()
    {
        // https://api.xuexiang.com.cn/index/index/test888/token/36b80acac6c085d3c445f931d0ed9c149d01b99e
        $where['source'] = '39元线下课包';

        $list = db('crm_customer')->where($where)->column('client_id');
        // var_dump($list);exit;
        foreach ($list as $key => $value) {
            db('crm_customer')->where('client_id',$value)->update(['date'=>time()]);
        }
    }

    public function test999()
    {
        // https://api.xuexiang.com.cn/index/test/test999/token/36b80acac6c085d3c445f931d0ed9c149d01b99e
        $list = db('crm_manage')->where(['type'=>3])->column('username');
        $customerList = db('crm_customer')->field('client_id,manage_id')->where(['manage_id'=>['in',implode(',', $list)]])->select();
        // var_dump($customerList);exit;
        foreach ($customerList as $key => $value) {
            db('crm_customer')->where('client_id',$value['client_id'])->update(['banzhuren_manage_id'=>$value['manage_id']]);
        }
        echo "运行完成";
    }

    public function customerManageDelete()
    {
        // https://api.xuexiang.com.cn/index/test/customerManageDelete/token/36b80acac6c085d3c445f931d0ed9c149d01b99e
        //今天记录昨天客户转移情况
        $starttime = date('Y-m-d',strtotime("-2 day"))." 00:00:00";
        $endtime = date('Y-m-d',strtotime("-2 day"))." 23:59:59";
        $where['date'] = array(array('egt',strtotime($starttime)),array('elt',strtotime($endtime)));
        $where['zhuanyi_date'] = array('neq','null');

        $customerList = db('crm_customer')->where($where)->column('client_id');
        var_dump($customerList);exit;
        db('crm_customer_manage')->where(['client_id'=>['in',implode(',', $customerList)]])->delete();
        echo "运行完成";
    }

    public function abcabc()
    {
        $datas = ['name'=>'1','position'=>1,'jump'=>1,'pic'=>'1','sort'=>1,'date'=>time(),'status'=>1];
        db('adver')->insert($datas);
        echo "完成";
    }

    public function sourceSet()
    {
        // https://api.xuexiang.com.cn/index/test/sourceSet/token/36b80acac6c085d3c445f931d0ed9c149d01b99e
        $list = db('crm_customer')->field('client_id,source,source_type')->where('source','2980元已付款')->select();
        var_dump($list);exit;
        foreach ($list as $key => $value) {
            $source = db('crm_media')->alias('a')->join('tp_crm_media_next b','a.id=b.media_id')->where(['b.type'=>$value['source_type']])->value('a.media');
            if (!empty($source)) {
                db('crm_customer')->where('client_id',$value['client_id'])->update(['source'=>$source]);
            }
        }
        echo "运行完成";
    }

    public function abcabca()
    {
        // https://api.xuexiang.com.cn/index/test/abcabca/token/36b80acac6c085d3c445f931d0ed9c149d01b99e

        $wx_tuwen = db('wx_tuwen')->select();
        var_dump($wx_tuwen);exit;
        foreach ($wx_tuwen as $key => $value) {
            db('wx_tuwen')->where('id',$value['id'])->update(['title'=>'第'.$value['id'].'节课程','url'=>'https://api.xuexiang.com.cn/gushici/index.html?id='.$value['id']]);
        }
        echo "运行完成";
    } 

    public function abcabcaa()
    {
        // https://api.xuexiang.com.cn/index/test/abcabcaa/token/36b80acac6c085d3c445f931d0ed9c149d01b99e
        $where['source_type'] = 0;
        $where['date'] = array(array('egt',strtotime("2023-05-01 00:00:00")),array('elt',strtotime("2023-06-31 23:59:59")));
        $list = db('crm_customer')->field('client_id,source,source_type')->where($where)->select();
        var_dump($list);exit;
        foreach ($list as $key => $value) {
            $source_id = db('crm_media')->where('media',$value['source'])->value('id');
            $source_type = db('crm_media_next')->where('media_id',$source_id)->value('type');
            if (!empty($source_type)) {
                db('crm_customer')->where('client_id',$value['client_id'])->update(['source_type'=>$source_type]);
            }
        }
        echo "运行完成";
    }

    public function aaabcv()
    {
        // https://api.xuexiang.com.cn/index/test/aaabcv/token/36b80acac6c085d3c445f931d0ed9c149d01b99e
        $phone = ['18792867556','18314119331','18932325610','15973624806','18798246176','15012204847','15285267561','13278076333','18662952631','13973844389','18292677062','13951477568','15556855065','15396691129','15937715896','13872789077','18821872140','18516211592','18306558739','18393333434','13575922502','13166969622','13873803468','17773682006','15309264960','18622363589','15929864618','13501470068','13246641366','13808214568','13708309083','18338191002','13852337897'];
        $where['phone'] = ['in',implode(',', $phone)];
        $where['public_paytype'] = 1;
        $list = db('crm_customer')->field('client_id,phone,source,source_type,public_paytype')->where($where)->select();
        // var_dump($list);exit;
        foreach ($list as $key => $value) {
            db('crm_customer')->where('client_id',$value['client_id'])->update(['public_paytype'=>3]);
        }
        echo "运行完成";
    }

    public function aaabcvv()
    {
        // https://api.xuexiang.com.cn/index/test/aaabcvv/token/36b80acac6c085d3c445f931d0ed9c149d01b99e
        $where['yaoyue_status'] = 6;
        $list = db('crm_customer')->field('client_id,yaoyue_status')->where($where)->select();
        var_dump($list);exit;
        foreach ($list as $key => $value) {
            db('crm_customer')->where('client_id',$value['client_id'])->update(['yaoyue_status'=>3]);
        }
        echo "运行完成";
    }

    public function aaabcvva()
    {
        // https://api.xuexiang.com.cn/index/test/aaabcvva/token/36b80acac6c085d3c445f931d0ed9c149d01b99e
        $where['source_type'] = ['in','1236'];
        $where['source'] = '39元公开课';
        $list = db('crm_customer')->field('client_id,source,source_type,FROM_UNIXTIME(date,"%Y-%m-%d %H:%i:%s") as date')->where($where)->select();
        var_dump($list);exit;
        foreach ($list as $key => $value) {
            db('crm_customer')->where('client_id',$value['client_id'])->update(['source'=>'0元公开课D']);
        }
        echo "运行完成";
    }

    public function next_course_kaibo_set()
    {
        // https://api.xuexiang.com.cn/index/test/next_course_kaibo_set/token/7df32de82d0a5e4d2599427eddcb4cee37db2bdb
        $list = db('goods_next')->select();
        foreach ($list as $key => $value) {
            if (strpos($value['desc'],'日') && strpos($value['desc'],'月') && strpos($value['desc'],'--')) {
                // if ($value['id'] <= 61) {
                //     $year = '2022';
                // }else{
                //     $year = date("Y",time());
                // }
                $shijian = explode('日', $value['desc']);
                $shijian1 = explode('月', $shijian[0]);
                $hourminute1 = explode('--', $shijian[1]);
                // var_dump(intval($shijian1[0]));exit;
                if ((intval($shijian1[0]) >= 8) && ($value['id'] <= 470)) {
                    $year = '2022';
                }else{
                    $year = date("Y",time());
                }
                $hourminute = trim($hourminute1[0]);
                $kaibo_time = $year.'-'.$shijian1[0].'-'.$shijian1[1].' '.$hourminute.':00';
                $datas['kaibo_time'] = strtotime($kaibo_time);
                db('goods_next')->where('id',$value['id'])->update($datas);
            }
        }
        echo "运行完成";
    }

    public function orderaaa()
    {
        // https://api.xuexiang.com.cn/index/test/orderaaa/token/7df32de82d0a5e4d2599427eddcb4cee37db2bdb
        $where['id'] = ['in','10079,10080,10081,10082'];
        $list = db('crm_order')->field('id,orderid,total')->where($where)->select();
        var_dump($list);exit;
        foreach ($list as $key => $value) {
            db('crm_order')->where('orderid',$value['orderid'])->delete();
            db('crm_order_goods')->where('order_id',$value['orderid'])->delete();
            db('crm_order_money')->where('order_id',$value['orderid'])->delete();
        }
        echo "运行完成";
    }

    public function orderaaaaaa()
    {
        // https://api.xuexiang.com.cn/index/test/orderaaaaaa/token/7df32de82d0a5e4d2599427eddcb4cee37db2bdb
        $where['formali_yingqi'] = '148';
        $where['banzhuren_manage_id'] = '70123';

        $list = db('crm_customer')->field('client_id,manage_id,banzhuren_manage_id')->where($where)->select();
        // var_dump($list);exit;
        foreach ($list as $key => $value) {
            if ($value['manage_id'] != $value['banzhuren_manage_id']) {
                var_dump($value);exit;
            }
        }
        echo "运行完成";
    }

    public function orderbbbbbb()
    {
        // https://api.xuexiang.com.cn/index/test/orderbbbbbb/token/7df32de82d0a5e4d2599427eddcb4cee37db2bdb
        $where['manage_id'] = '12007';

        $list = db('crm_customer')->field('client_id,manage_id')->where($where)->select();
        // var_dump($list);exit;
        foreach ($list as $key => $value) {
            db('crm_customer')->where('client_id',$value['client_id'])->update(['manage_id'=>'1999']);
        }
        echo "运行完成";
    }


    public function orderbbbbbbc()
    {
        // https://api.xuexiang.com.cn/index/test/orderbbbbbbc/token/7df32de82d0a5e4d2599427eddcb4cee37db2bdb

        $where['date'] = array(array('egt',strtotime("2023-06-01 00:00:00")),array('elt',strtotime("2023-06-30 23:59:59")));
        $where['source'] = '3元公开课M';
        $customerList = db('crm_customer')->where($where)->limit(3200,800)->select();
        var_dump($customerList);exit();
        $array[0] = array('客户ID','姓名','电话','年级','省市区','地址','数据来源','创建时间','邀约情况','学习营期','归属人','班主任','首课进度','二课进度','来源type值');
        $yaoyue_status=[0=>'',1=>'已加微信',2=>'已接听未加微',3=>'未接听',4=>'退费',5=>'空号或非本人',6=>'5次以上未接听'];
        $yingqi_status=[0=>'未上课',1=>'10分钟内',2=>'半小时内',3=>'1小时内',4=>'2小时内',5=>'已完课',6=>'已交作业'];
        foreach ($customerList as $key => $value) {
            $customerList[$key]['province'] = db('crm_region')->where('region_id',$value['province'])->value('region_name');
            $customerList[$key]['city'] = db('crm_region')->where('region_id',$value['city'])->value('region_name');
            $customerList[$key]['area'] = db('crm_region')->where('region_id',$value['area'])->value('region_name');
            $pca = $customerList[$key]['province'].$customerList[$key]['city'].$customerList[$key]['area'];
            $manage_name = db('crm_manage')->where('username',$value['manage_id'])->value('name');
            $banzhuren_name = db('crm_manage')->where('username',$value['banzhuren_manage_id'])->value('name');
            if (!empty($value['public_yingqi'])) {
                    $customerList[$key]['yingqi'] = db('goods')->where('id',$value['public_yingqi'])->value('title');
                    if (!empty($customerList[$key]['yingqi'])) {

                        if (strpos($customerList[$key]['yingqi'],'G')) {
                            $yingqi = explode('G', $customerList[$key]['yingqi']);
                            $customerList[$key]['yingqi'] = $yingqi[0].'G';
                        }
                        if (strpos($customerList[$key]['yingqi'], 'D')) {
                            $yingqi = explode('D', $customerList[$key]['yingqi']);
                            $customerList[$key]['yingqi'] = $yingqi[0].'D';
                        }
                        if ($customerList[$key]['yingqi'] == '国际音标速成训练课' || $customerList[$key]['yingqi'] == '中小学生中外名著赏读') {
                            $customerList[$key]['yingqi'] = '无营期';
                        }
                        $next_course = db('goods_next')->field('id')->where('goods_id',$value['public_yingqi'])->limit(0,2)->order('id ASC')->select();
                        if (count($next_course) == 2) {
                            $public_yingqi_course1 = $next_course[0]['id'];
                            $public_yingqi_course2 = $next_course[1]['id'];
                        }else{
                            $public_yingqi_course1 = '';
                            $public_yingqi_course2 = '';
                        }
                    }else{
                        $customerList[$key]['yingqi'] = '无营期';
                    }
                    if (!empty($public_yingqi_course1)) {
                        $yingqi_status1 = db('crm_durations')->field('status,zuoye')->where(array('course_id'=>$public_yingqi_course1,'phone'=>$value['phone']))->find();
                    }else{
                        $customerList[$key]['yingqi_status1'] = '';
                        $customerList[$key]['yingqi_status1_zuoye'] = '';
                    }
                    if (empty($yingqi_status1)) {
                        $customerList[$key]['yingqi_status1'] = '';
                        $customerList[$key]['yingqi_status1_zuoye'] = '';
                    }else{
                        $customerList[$key]['yingqi_status1'] = $yingqi_status1['status'];
                        $customerList[$key]['yingqi_status1_zuoye'] = $yingqi_status1['zuoye'];
                    }
                    if (!empty($public_yingqi_course2)) {
                        $yingqi_status2 = db('crm_durations')->field('status,zuoye')->where(array('course_id'=>$public_yingqi_course2,'phone'=>$value['phone']))->find();
                    }else{
                        $customerList[$key]['yingqi_status2'] = '';
                        $customerList[$key]['yingqi_status2_zuoye'] = '';
                    }
                    if (empty($yingqi_status2)) {
                        $customerList[$key]['yingqi_status2'] = '';
                        $customerList[$key]['yingqi_status2_zuoye'] = '';
                    }else{
                        $customerList[$key]['yingqi_status2'] = $yingqi_status2['status'];
                        $customerList[$key]['yingqi_status2_zuoye'] = $yingqi_status2['zuoye'];
                    }
            }else{
                $customerList[$key]['yingqi'] = '无营期';
                $customerList[$key]['yingqi_status1'] = '';
                $customerList[$key]['yingqi_status2'] = '';
                $customerList[$key]['yingqi_status1_zuoye'] = '';
                $customerList[$key]['yingqi_status2_zuoye'] = '';
            }
            if (empty($customerList[$key]['yingqi_status1'])) {
                $customerList[$key]['yingqi_status1'] = 0;
                $customerList[$key]['yingqi_status1_zuoye'] = 0;
            }
            if (empty($customerList[$key]['yingqi_status2'])) {
                $customerList[$key]['yingqi_status2'] = 0;
                $customerList[$key]['yingqi_status2_zuoye'] = 0;
            }
            $array[] = array($value['client_id'],$value['name'],$value['phone'],$value['grade'],$pca,$value['address'],$value['source'],date("Y-m-d H:i:s",$value['date']),$yaoyue_status[$value['yaoyue_status']],$customerList[$key]['yingqi'],$manage_name,$banzhuren_name,$yingqi_status[$customerList[$key]['yingqi_status1']],$yingqi_status[$customerList[$key]['yingqi_status2']],$value['source_type']);
        }
        excels::index($array,'客户列表 '.date("Y-m-d h.i.s",time()),'','C,E,H',0,15,30,25,30);
    }

    public function aaa123()
    {
        // https://api.xuexiang.com.cn/index/test/aaa123/token/7df32de82d0a5e4d2599427eddcb4cee37db2bdb
        $repeat_count = db('cooperation')->where(array('number'=>['in','0,0.1,1,3,9.9,19.9,39,69'],'phone'=>'15317896885'))->count();
        // $repeat_count = db('cooperation')->where(['number'=>['in','0,0.1,1,3,9.9,19.9,39,69']])->where(['phone'=>'15317896885'])->count();
        var_dump($repeat_count);exit;
    }

    public function aaa123456()
    {
        // https://api.xuexiang.com.cn/index/test/aaa123456/token/7df32de82d0a5e4d2599427eddcb4cee37db2bdb
        $data['user_id'] = '260030';
        $data['type'] = '457';
        if (($data['type'] == 457) || ($data['type'] == 460) || ($data['type'] == 461)) {
            $exist = db('user_ticket')->where(['user_id'=>$data['user_id'],'type'=>array('in','457,460,461'),'status'=>1])->order('date DESC')->find();
        }
        if (!empty($exist)) {
            $time = intval(time()-$exist['date']);
            var_dump($time);exit;
            if (intval(time()-$exist['date']) < 5*24*3600) {
                $status = 1;
            }
        }
    }

    public function aaa123123()
    {
        //https://api.xuexiang.com.cn/index/test/aaa123123/token/7df32de82d0a5e4d2599427eddcb4cee37db2bdb
        $where['date'] = array(array('egt',strtotime("2023-07-22 00:00:00")),array('elt',strtotime("2023-07-23 23:59:59")));
        //$where['type'] = ['in','457,460,461'];
        $ticket_list = db('user_ticket')->where($where)->select();
        $num = 0;
        foreach ($ticket_list as $key => $value) {
            $phone = db('user')->where('id',$value['user_id'])->value('phone');
            if (!empty($phone)) {
                db('crm_customer')->where('phone',$phone)->update(['ticket'=>1]);
                $num++;
            }
        }

        var_dump($num);exit;
    }

    public function aaa1235698()
    {
        //https://api.xuexiang.com.cn/index/test/aaa1235698/token/7df32de82d0a5e4d2599427eddcb4cee37db2bdb
        $where['date'] = array(array('egt',strtotime("2023-07-24 18:00:00")),array('elt',strtotime("2023-07-24 23:59:59")));
        $where['source'] = '0元公开课';
        $list = db('crm_customer')->where($where)->select();
        foreach ($list as $key => $value) {
            db('crm_customer')->where('client_id',$value['client_id'])->update(['source'=>'0元进校H']);
        }
        echo "完成";
    }

    public function aaa12343121()
    {
        //https://api.xuexiang.com.cn/index/test/aaa12343121/token/7df32de82d0a5e4d2599427eddcb4cee37db2bdb
        $datas = array(
            'client_id'=>'196909',
            'phone'=>'15270826615',
            'name'=>'无姓名',
            'grade'=>'年级'
        );
        try {
            db('crm_customer')->insert($datas);
            echo "插入成功";
        } catch (Exception $e) {
            echo "插入失败";
        }
    }

    public function aaa123431211()
    {
        //https://api.xuexiang.com.cn/index/test/aaa123431211/token/7df32de82d0a5e4d2599427eddcb4cee37db2bdb
        db('crm_customer')->where('client_id',200486)->delete();
        echo "删除成功";
    }

    public function runCustomerOrder()
    {
        //https://api.xuexiang.com.cn/index/test/runCustomerOrder/token/7df32de82d0a5e4d2599427eddcb4cee37db2bdb
        $where['id'] = ['<',88];
        $where['search_type'] = ['in','4,5,6'];
        $goods = db('goods')->field('id,title')->where($where)->order('id DESC')->select();
        $number = ['all'=>0,'baoke'=>0,'xuke'=>0];
        foreach ($goods as $key => $value) {
            $goods_phone = db('goods_phone')->where('course_id',$value['id'])->column('phone');
            foreach ($goods_phone as $key1 => $value1) {
                $number['all']++;
                $customer = db('crm_customer')->where('phone',$value1)->find();
                if (!empty($customer)) {
                    $customer_datas = [];
                    if (empty($customer['formali_yingqi'])) {
                        $customer_datas['formali_yingqi'] = $value['id'];
                    }
                    $order_count = db('crm_order')->where(['phone'=>$value1,'total'=>['>=',1980],'order_status'=>['neq','1,6,7,8']])->count();
                    if (!empty($order_count)) {
                        $number['baoke']++;
                        $customer_datas['jieye_star'] = 5;
                        $customer_datas['is_jieye'] = 1;
                        $customer_datas['jieye_time'] = time();
                        if (empty($customer['xuke_label'])) {
                            $customer_datas['xuke_label'] = 2;
                        }
                        if ($order_count >= 2) {
                            $number['xuke']++;
                            $customer_datas['xuke_label'] = 1;
                        }
                        try {
                            db('crm_customer')->where('phone',$value1)->update($customer_datas);
                        } catch (Exception $e) {
                            echo $value1."更新失败";
                        }
                    }
                }
            }
        }
        var_dump($number);exit;
        // array(3) { ["all"]=> int(4460) ["baoke"]=> int(4198) ["xuke"]=> int(474) }
    }

    public function runCustomerOrdera()
    {
        //https://api.xuexiang.com.cn/index/test/runCustomerOrdera/token/7df32de82d0a5e4d2599427eddcb4cee37db2bdb
        $where['jieye_time'] = ['<',1690473600];
        $where['is_jieye'] = 1;
        $where['xuke_label'] = 2;
        $list = db('crm_customer')->where($where)->select();
        $order_where['total'] = ['between',[1900,3000]];
        // var_dump($list);exit;
        $number = 0;
        foreach ($list as $key => $value) {
            $order_where['phone'] = $value['phone'];
            $order = db('crm_order')->where($order_where)->find();
            if (empty($value['jiaowu_manage_id'])) {
                if (!empty($order)) {
                    $manage = db('crm_manage')->where('username',$order['manage_id'])->find();
                    if (!empty($manage)) {
                        $customer_datas['jiaowu_manage_id'] = $order['manage_id'];
                    }else{
                        $customer_datas['jiaowu_manage_id'] = '10000';
                    }
                }
            }else{
                if ($value['jiaowu_manage_id'] != $order['manage_id']) {
                    $manage = db('crm_manage')->where('username',$order['manage_id'])->find();
                    $fenzu = db("crm_fenzu")->where('id',$manage['zu_id'])->find();
                    if (!empty($fenzu)) {
                        if ($fenzu['pid'] != 0) {
                            $zu_manage = db('crm_manage')->where('zu_id',$fenzu['pid'])->find();
                            $customer_datas['jiaowu_manage_id'] = $zu_manage['username'];
                        }
                    }
                }
            }
            if (!empty($customer_datas)) {
                $number++;
                db('crm_customer')->where('client_id',$value['client_id'])->update($customer_datas);
            }
        }
        var_dump($number);exit;
        // 3240
    }

    public function findOrderGoods()
    {
        //https://api.xuexiang.com.cn/index/test/findOrderGoods/token/7df32de82d0a5e4d2599427eddcb4cee37db2bdb

        $where2['order_status'] = ['in',[2,3,4,5]];
        $where2['date'] = ['between',[strtotime('2023-07-18 00:00:00'),strtotime('2023-07-31 00:00:00')]];
        $where2['total'] = '1980';
        $list = db('crm_order')->where($where2)->count();
        var_dump($list);exit;


        $where['b.goods_id'] = 16;//718活动2980系统班（高）
        $where['a.order_status'] = ['neq','1,6,7,8'];
        $list = db('crm_order')->alias('a')->join('tp_crm_order_goods b','a.orderid=b.order_id')->distinct(true)->field('a.total')->where($where)->select();
        

        $where1['b.goods_id'] = 17;//718活动2980系统班（高）
        $where1['a.order_status'] = ['neq','1,6,7,8'];
        $list1 = db('crm_order')->alias('a')->join('tp_crm_order_goods b','a.orderid=b.order_id')->distinct(true)->field('a.total')->where($where1)->select();
        var_dump($list);var_dump($list1);exit;
    }

    /**
     * curl函数封装
     * @param  $method        请求方式GET、POST
     * @param  $url           请求接口地址
     * @param  $data          提交数据 　
     * @return array
     */
    public function httpGet1($url, $method = 'get', $data = '')
    {
        $ch = curl_init();//初始化
        $headers = array("Content-type:application/json","Accept:application/json");
        //设置URL和相应的选项
        curl_setopt($ch, CURLOPT_URL, $url);//指定请求的URL
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, strtoupper($method));//提交方式
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);//不验证SSL
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);//不验证SSL
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);//设置HTTP头字段的数组
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible;MSIE 5.01;Windows NT 5.0)');//头的字符串
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_AUTOREFERER, 1);//自动设置header中的Referer:信息
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);//提交数值
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);//是否输出到屏幕上,不直接输出
        $temp = curl_exec($ch);//执行并获取结果
        curl_close($ch);
        return $temp;//return 返回值
    }

    public function getAccessToken()
    {
        $appid = 'wx72e7847409233591';
        $secret = 'eb98aa1a381259fd9600cedfc8ddf8f5';
        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$secret."";
        $result = self::httpGet1($url,'GET');
        $result = json_decode($result,true);
        var_dump($result);exit;
        // 71_nKxwGHGvYZOeaob4oW6r9wFEBX8dEo4HfvBFUqOOy37cMUudHlgrmdDfkNGVZI8cbOrfLvvCkrOsnTay_26oZqkoBIFfbgFrUS_OwZHMzJGwMXdM0IJwPLPdNFwXGIaADAIRT
    }

    public function getShopList()
    {
        $url = "https://api.weixin.qq.com/channels/ec/order/list/get?access_token=71_nKxwGHGvYZOeaob4oW6r9wFEBX8dEo4HfvBFUqOOy37cMUudHlgrmdDfkNGVZI8cbOrfLvvCkrOsnTay_26oZqkoBIFfbgFrUS_OwZHMzJGwMXdM0IJwPLPdNFwXGIaADAIRT";
        $datas = [
            'create_time_range'=>[
                'start_time'=>1670515200,
                'end_time'=>1670601599
            ],
            'page_size'=>10
        ];
        $result = self::httpGet1($url,'POST',json_encode($datas));
        $result = json_decode($result,true);
        var_dump($result);exit;
        // array(5) { ["errcode"]=> int(0) ["errmsg"]=> string(2) "ok" ["order_id_list"]=> array(1) { [0]=> string(19) "3708015871050515712" } ["next_key"]=> string(84) "CiUI6/Poqg4gAigKOABIAVIMCICUyJwGEP+2zZwG+AEBgAIAiAIBEICK6PuemeG6MxiGssucBiCWssucBigC" ["has_more"]=> bool(false) }
    }

    public function getOrderInfo()
    {
        $url = "https://api.weixin.qq.com/channels/ec/order/get?access_token=71_nKxwGHGvYZOeaob4oW6r9wFEBX8dEo4HfvBFUqOOy37cMUudHlgrmdDfkNGVZI8cbOrfLvvCkrOsnTay_26oZqkoBIFfbgFrUS_OwZHMzJGwMXdM0IJwPLPdNFwXGIaADAIRT";
        $datas = [
            'order_id'=>'3708015871050515712'
        ];
        $result = self::httpGet1($url,'POST',json_encode($datas));
        $result = json_decode($result,true);
        var_dump($result);exit;
        // array(3) { ["errcode"]=> int(0) ["errmsg"]=> string(2) "ok" ["order"]=> array(7) { ["order_id"]=> string(19) "3708015871050515712" ["create_time"]=> int(1670568198) ["update_time"]=> int(1670568214) ["status"]=> int(250) ["order_detail"]=> array(8) { ["product_infos"]=> array(1) { [0]=> array(15) { ["product_id"]=> string(14) "10000006688308" ["sku_id"]=> string(9) "592477436" ["thumb_img"]=> string(126) "https://store.mp.video.tencent-cloud.com/161/20304/snscosdownload/SH/reserved/638715c900040d1f167f37afd8118e0b000000a000004f50" ["sale_price"]=> int(3900) ["sku_cnt"]=> int(1) ["title"]=> string(41) "2天4节超强记忆力训练营直播课" ["on_aftersale_sku_cnt"]=> int(0) ["finish_aftersale_sku_cnt"]=> int(0) ["sku_code"]=> string(0) "" ["market_price"]=> int(3900) ["sku_attrs"]=> array(1) { [0]=> array(2) { ["attr_key"]=> string(12) "适用人群" ["attr_value"]=> string(45) "适用于家长、成人、考试考证人群" } } ["real_price"]=> int(3900) ["out_warehouse_id"]=> string(0) "" ["sku_deliver_info"]=> array(1) { ["stock_type"]=> int(0) } ["extra_service"]=> array(2) { ["seven_day_return"]=> int(0) ["freight_insurance"]=> int(0) } } } ["pay_info"]=> array(2) { ["prepay_id"]=> string(36) "wx091443194383921c6453df7ef241c60000" ["prepay_time"]=> int(1670568199) } ["price_info"]=> array(3) { ["product_price"]=> int(3900) ["order_price"]=> int(3900) ["freight"]=> int(0) } ["delivery_info"]=> array(4) { ["address_info"]=> array(11) { ["user_name"]=> string(0) "" ["postal_code"]=> string(0) "" ["province_name"]=> string(0) "" ["city_name"]=> string(0) "" ["county_name"]=> string(0) "" ["detail_info"]=> string(0) "" ["national_code"]=> string(0) "" ["tel_number"]=> string(0) "" ["house_number"]=> string(0) "" ["virtual_order_tel_number"]=> string(11) "15356961829" ["use_tel_number"]=> int(0) } ["delivery_product_info"]=> array(0) { } ["ship_done_time"]=> int(0) ["deliver_method"]=> int(1) } ["ext_info"]=> array(2) { ["customer_notes"]=> string(0) "" ["merchant_notes"]=> string(0) "" } ["commission_infos"]=> array(0) { } ["settle_info"]=> array(1) { ["predict_commission_fee"]=> int(0) } ["sku_sharer_infos"]=> array(0) { } } ["aftersale_detail"]=> array(2) { ["aftersale_order_list"]=> array(0) { } ["on_aftersale_order_cnt"]=> int(0) } ["openid"]=> string(28) "o6zla5YgaAgzw3aC8SUWaxL1kBzc" } }
    }

    public function aaadsd()
    {
        // xitongnew type
        if (isset($data['xitongnew'])) {
            if (!empty($data['xitongnew'])) {
                $datas['xitongnew'] = $data['xitongnew'];
                if (!isset($source_info)) {
                    $source_info = $this->get_link_info($data['type']);
                }
                if ($source_info) {
                    $datas['source_id'] = $source_info['source_id'];
                    $datas['source_type'] = $source_info['source_type'];
                    $datas['manage_id'] = $source_info['manage_id'];
                }
            }
        }

        if (isset($data['xitongnew'])) {
            if (!empty($data['xitongnew'])) {
                $userdatas['xitongnew'] = $data['xitongnew'];
                if (!isset($source_info)) {
                    $source_info = $this->get_link_info($data['type']);
                }
                if ($source_info) {
                    $userdatas['source_id'] = $source_info['source_id'];
                    $userdatas['source_type'] = $source_info['source_type'];
                    $userdatas['manage_id'] = $source_info['manage_id'];
                }
            }
        }

        if (isset($data['xitongnew'])) {
            if (!empty($data['xitongnew'])) {
                $user_pay_datas['xitongnew'] = $data['xitongnew'];
                if (!isset($source_info)) {
                    $source_info = $this->get_link_info($data['type']);
                }
                if ($source_info) {
                    $user_pay_datas['source_id'] = $source_info['source_id'];
                    $user_pay_datas['source_type'] = $source_info['source_type'];
                    $user_pay_datas['manage_id'] = $source_info['manage_id'];
                }
            }
        }

        if (isset($data['xitongnew'])) {
            if (!empty($data['xitongnew'])) {
                $cooperation['xitongnew'] = $data['xitongnew'];
                if (!isset($source_info)) {
                    $source_info = $this->get_link_info($data['type']);
                }
                if ($source_info) {
                    $cooperation['source_id'] = $source_info['source_id'];
                    $cooperation['source_type'] = $source_info['source_type'];
                    $cooperation['manage_id'] = $source_info['manage_id'];
                }
            }
        }

        // 支付回调
        // ,source_id,source_type,manage_id

        if (isset($payInfo['xitongnew'])) {
            if (!empty($payInfo['xitongnew'])) {
                $cooperation['xitongnew'] = $payInfo['xitongnew'];
                $cooperation['source_id'] = $payInfo['source_id'];
                $cooperation['source_type'] = $payInfo['source_type'];
                $cooperation['manage_id'] = $payInfo['manage_id'];
            }
        }

        // 优惠券
        if (isset($data['xitongnew'])) {
            if (!empty($data['xitongnew'])) {
                $datas['xitongnew'] = $data['xitongnew'];
            }
        }


        // if (isset($data['xitongnew'])) {
        //     if (!empty($data['xitongnew'])) {
        //         $user_pay_datas['xitongnew'] = $data['xitongnew'];
        //         if (empty($data['source_id']) || empty($data['source_type']) || empty($data['manage_id'])) {
        //             $this->apiReturn1(201,'参数不完整','');exit;
        //         }
        //         if (!isset($source_info)) {
        //             $source_info = $this->get_link_info($data['source_id'], $data['source_type'], $data['manage_id']);
        //         }
        //         $user_pay_datas['source_id'] = $data['source_id'];
        //         $user_pay_datas['source_type'] = $data['source_type'];
        //         $user_pay_datas['manage_id'] = $data['manage_id'];
        //         $user_pay_datas['source_name'] = $source_info['source_name'];
        //         $user_pay_datas['source_type_name'] = $source_info['source_type_name'];
        //         $user_pay_datas['manage_name'] = $source_info['manage_name'];
        //     }
        // }
    }

    public function get_link_info($type)
    {
        if (empty($type)) {
            return false;
        }
        $link = db('link')->where('type',$type)->find();
        if (empty($link)) {
            return false;
        }
        return $link;
    }

    public function findCustomer()
    {
        // 杨总 2980已成交里的客户里面剔除4月份以后的数据，剔除已成交3280的客户，把剩余的生成一张表，最后将4月份前结业数据进行分配，按个数分配，不够的补充，多的不需要补充
        // https://api.xuexiang.com.cn/index/test/findCustomer/token/7df32de82d0a5e4d2599427eddcb4cee37db2bdb
        // $url = "https://xuexiang.oss-cn-hangzhou.aliyuncs.com/%E6%B1%87%E6%80%BB%E6%88%90%E4%BA%A4%E4%B8%80%E7%BB%84.xlsx";//一组
        // $url = "https://xuexiang.oss-cn-hangzhou.aliyuncs.com/%E6%B1%87%E6%80%BB%E6%88%90%E4%BA%A4%E4%BA%8C%E7%BB%84.xlsx";//二组
        $url = "https://xuexiang.oss-cn-hangzhou.aliyuncs.com/1691976761.xlsx";//陈超组
        // $url = "https://xuexiang.oss-cn-hangzhou.aliyuncs.com/1692085227.xlsx";//五组
        // var_dump($url);exit;
        $data_array = excels::readExcelFile($url);
        // var_dump($data_array);exit;
        if (empty($data_array)) {
            apiReturn(204,'文件内容为空或读取失败');
        }
        $empty_customer = [];//不存在的客户
        $dayu4yue_customer = [];//大于4月份的客户
        $ycj_customer = [];//已成交3280客户
        $phones = [];//所有剔除客户
        $array[0] = ['客户名称','电话号码','数据微信号','说明'];
        foreach ($data_array as $key => &$value) {
            if ($key >= 1) {
                if (!empty($value[1])) {
                    $customer = db('crm_customer')->where('phone',$value[1])->find();
                    if (empty($customer)) {
                        $empty_customer[] = $value[1];
                        if (!in_array($value[1],$phones)) {
                            $phones[] = $value[1];
                            $value[3] = '不存在的客户';
                        }
                        // unset($data_array[$key]);
                        continue;
                    }else{
                        if ($customer['date'] >= 1680278400) {
                            $dayu4yue_customer[] = $value[1];
                            if (!in_array($value[1],$phones)) {
                                $phones[] = $value[1];
                                $value[3] = '大于4月份的客户';
                            }
                            // unset($data_array[$key]);
                            continue;
                        }
                        $order_count = db('crm_order')->where(['phone'=>$value[1],'total'=>['>=',1980],'order_status'=>['neq','1,6,7,8']])->count();
                        if ($order_count >= 2) {
                            $ycj_customer[] = $value[1];
                            if (!in_array($value[1],$phones)) {
                                $phones[] = $value[1];
                                $value[3] = '已成交3280客户';
                            }
                            // unset($data_array[$key]);
                            continue;
                        }
                    }
                }
            }
        }
        foreach ($data_array as $key => $value) {
            $array[] = $value;
        }
        excels::index($array,'列表 '.date("Y-m-d h.i.s",time()),'','',0,20,30,25,30);

        // echo "不存在的客户 ".count($empty_customer).'条';
        // echo "<hr>";
        // foreach ($empty_customer as $key => $value) {
        //     echo $value."<br>";
        // }
        // echo "<hr>";
        // echo "大于4月份的客户 ".count($dayu4yue_customer).'条';
        // echo "<hr>";
        // foreach ($dayu4yue_customer as $key => $value) {
        //     echo $value."<br>";
        // }
        // echo "<hr>";
        // echo "已成交3280客户 ".count($ycj_customer).'条';
        // echo "<hr>";
        // foreach ($ycj_customer as $key => $value) {
        //     echo $value."<br>";
        // }
        // echo "<hr>";
        // echo "所有剔除客户 ".count($phones).'条';
        // echo "<hr>";
        // foreach ($phones as $key => $value) {
        //     echo $value."<br>";
        // }
        // echo "<hr>";
        // exit;
    }

    public function insertCustomer()
    {
        // $where['phone'] = null;
        // $list = db('crm_customer_baoke')->where($where)->select();
        // var_dump($list);exit;
        // https://api.xuexiang.com.cn/index/test/insertCustomer/token/7df32de82d0a5e4d2599427eddcb4cee37db2bdb

        // $url = "https://xuexiang.oss-cn-hangzhou.aliyuncs.com/1691982851.xlsx";//一组
        // $url = "https://xuexiang.oss-cn-hangzhou.aliyuncs.com/1691982882.xlsx";//二组
        // $url = "https://xuexiang.oss-cn-hangzhou.aliyuncs.com/1691982895.xlsx";//陈超组
        $url = "https://xuexiang.oss-cn-hangzhou.aliyuncs.com/1692085692.xlsx";//五组
        $zu_type = 4;//1一组 2二组 3陈超组 4五组

        $data_array = excels::readExcelFile($url);
        if (empty($data_array)) {
            apiReturn(204,'文件内容为空或读取失败');
        }
        foreach ($data_array as $key => $value) {
            if ($key >= 1) {
                $datas = array(
                    'phone'=>$value[5],
                    'zu_type'=>$zu_type
                );
                db('crm_customer_baoke')->insert($datas);
            }
        }
        echo "运行完成";
    }

    public function setCustomerBaoke()
    {
        // https://api.xuexiang.com.cn/index/test/setCustomerBaoke/token/7df32de82d0a5e4d2599427eddcb4cee37db2bdb

        $where['zu_type'] = 2;
        $list = db('crm_customer_baoke')->where($where)->select();
        foreach ($list as $key => $value) {
            db('crm_customer_baoke')->where('id',$value['id'])->update(['zu_type'=>1]);
        }
        echo "运行完成";
    }

    public function findJieye()
    {
        // https://api.xuexiang.com.cn/index/test/findJieye/token/7df32de82d0a5e4d2599427eddcb4cee37db2bdb
        $where['jieye_time'] = ['between',[strtotime('2023-06-01 00:00:00'),strtotime('2023-07-28 23:59:59')]];
        $list = db('crm_customer')->field('client_id,jiaowu_manage_id,phone')->where($where)->select();
        // var_dump($list);exit;
        foreach ($list as $key => $value) {
            $exist = db('crm_customer_baoke')->where('phone',$value['phone'])->find();
            if (!empty($exist)) {
                db('crm_customer_baoke')->where('phone',$value['phone'])->update(['status'=>1]);
            }
        }
        echo "运行完成";
    }

    public function find_customer_baoke()
    {
        // https://api.xuexiang.com.cn/index/test/find_customer_baoke/token/7df32de82d0a5e4d2599427eddcb4cee37db2bdb
        $datas['count1'] = db('crm_customer_baoke')->where(['zu_type'=>1,'status'=>1])->count();
        $datas['count2'] = db('crm_customer_baoke')->where(['zu_type'=>2,'status'=>1])->count();
        $datas['count3'] = db('crm_customer_baoke')->where(['zu_type'=>3,'status'=>1])->count();
        $datas['count4'] = db('crm_customer_baoke')->where(['zu_type'=>4,'status'=>1])->count();
        var_dump($datas);exit;
        // 4173 1043
        // 10000 20000 34000 35000
        // 934 0 605 480
        // 110  1043  438 563
        // int(278) int(656) int(605) int(480)
        // 4173-1320
        // array(3) { ["count1"]=> int(279) ["count2"]=> int(1320) ["count3"]=> int(607) }
        // array(4) { ["count1"]=> int(279) ["count2"]=> int(1320) ["count3"]=> int(607) ["count4"]=> int(481) }
    }

    public function fenpei()
    {
        // https://api.xuexiang.com.cn/index/test/fenpei/token/7df32de82d0a5e4d2599427eddcb4cee37db2bdb
        // exit;
        // 110  1043  438 563
        $phones = db('crm_customer_baoke')->where('status',1)->column('phone');
        $where['phone'] = ['notin',$phones];
        $where['jieye_time'] = ['between',[strtotime('2023-06-01 00:00:00'),strtotime('2023-07-28 23:59:59')]];
        $list = db('crm_customer')->field('client_id,jiaowu_manage_id,phone')->where($where)->select();
        $number1 = 0;
        $number2 = 0;
        $number3 = 0;
        $jiaowu_manage_id = [1=>'10000',2=>'20000',3=>'34000',4=>'35000'];
        foreach ($list as $key => $value) {
            // db('crm_customer')->where('client_id',$value['client_id'])->update(['jiaowu_manage_id'=>'99999']);
            $exist = db('crm_customer_baoke')->where(['phone'=>$value['phone'],'status'=>1])->find();
            if (empty($exist)) {
                if ($value['jiaowu_manage_id'] != '10000' && $value['jiaowu_manage_id'] != '20000' && $value['jiaowu_manage_id'] != '34000') {
                    if ($number1 < 563) {
                        db('crm_customer')->where('client_id',$value['client_id'])->update(['jiaowu_manage_id'=>$jiaowu_manage_id[4]]);
                        $number1++;
                        // continue;
                    }
                }
            }
            //     // if ($number2 <= 71) {
            //     //     db('crm_customer')->where('client_id',$value['client_id'])->update(['jiaowu_manage_id'=>$jiaowu_manage_id[2]]);
            //     //     $number2++;
            //     //     continue;
            //     // }
            //     // if ($number3 <= 784) {
            //     //     db('crm_customer')->where('client_id',$value['client_id'])->update(['jiaowu_manage_id'=>$jiaowu_manage_id[3]]);
            //     //     $number3++;
            //     //     continue;
            //     // }
            // }else{
            //     db('crm_customer')->where('client_id',$value['client_id'])->update(['jiaowu_manage_id'=>$jiaowu_manage_id[$exist['zu_type']]]);
            //     // $number1++;
            // }
            // if ($number1 <= 1391) {
            //     db('crm_customer')->where('client_id',$value['client_id'])->update(['jiaowu_manage_id'=>$jiaowu_manage_id[1]]);
            //     $number1++;
            //     // continue;
            // }
        }
        echo "运行完成";
    }

    public function find_customer_baoke1()
    {
        // https://api.xuexiang.com.cn/index/test/find_customer_baoke1/token/7df32de82d0a5e4d2599427eddcb4cee37db2bdb
        $where['jieye_time'] = ['between',[strtotime('2023-06-01 00:00:00'),strtotime('2023-07-28 23:59:59')]];
        $list = db('crm_customer')->field('client_id,jiaowu_manage_id,phone')->where($where)->select();
        $number1 = 0;
        $number2 = 0;
        $number3 = 0;
        $number4 = 0;
        foreach ($list as $key => $value) {
            if ($value['jiaowu_manage_id'] == '10000') {
                $number1++;
            }
            if ($value['jiaowu_manage_id'] == '20000') {
                $number2++;
            }
            if ($value['jiaowu_manage_id'] == '34000') {
                $number3++;
            }
            if ($value['jiaowu_manage_id'] == '35000') {
                $number4++;
            }
        }
        var_dump($number1);
        var_dump($number2);
        var_dump($number3);
        var_dump($number4);
        exit;
    }

    public function findGrade()
    {
        // https://api.xuexiang.com.cn/index/test/findGrade/token/7df32de82d0a5e4d2599427eddcb4cee37db2bdb

        $url = "https://xuexiang.oss-cn-hangzhou.aliyuncs.com/1692004675.xlsx";

        $data_array = excels::readExcelFile($url);
        if (empty($data_array)) {
            apiReturn(204,'文件内容为空或读取失败');
        }
        // var_dump($data_array);exit;
        $array[0] = array('电话','系统年级','填写年级');
        foreach ($data_array as $key => $value) {
            if ($key >= 1) {
                $grade = db('crm_customer')->where('phone',$value[2])->value('grade');
                if (!empty($grade)) {
                    if ($grade != $value[7]) {
                        $array[] = [$value[2],$grade,$value[7]];
                    }
                }
            }
        }
        excels::index($array,'年级错误客户列表 '.date("Y-m-d h.i.s",time()),'','A',0,15,30,25,30);
        // echo "运行完成";
    }

    public function setnoInsertCustomer($phone)
    {
        // https://api.xuexiang.com.cn/index/test/setnoInsertCustomer/token/7df32de82d0a5e4d2599427eddcb4cee37db2bdb
        // $list = db('crm_customer_noinsert')->where('status',1)->select();
        // $array = [];
        // foreach ($list as $key => $value) {
            $exist = db('crm_wxcode_phone')->where('phone',$phone)->find();
            if (!empty($exist)) {
                $customer = db('crm_customer')->where('phone',$exist['phone'])->find();
                if ($customer['manage_id'] == '99999') {
                    $manage_type = db('crm_manage')->where('username',$exist['manage_id'])->value('type');
                    if ($manage_type == 2) {
                        $customer_datas['jiaowu_manage_id'] = $exist['manage_id'];
                        $customer_datas['jiaowu_zhuanyi_date'] = time();
                        //添加邀约老师到客户信息
                        $where['jiaowus'] = array('like','%'.$exist['manage_id'].'%');
                        $yaoyue_manage_id = db('crm_manage')->where($where)->value('username');
                        if (!empty($yaoyue_manage_id)) {
                            $customer_datas['yaoyue_manage_id'] = $yaoyue_manage_id;
                            $customer_datas['yaoyue_zhuanyi_date'] = time();
                        }else{
                            $customer_datas['yaoyue_manage_id'] = '';
                        }
                    }
                    $customer_datas['fenpei_date'] = time();
                    $customer_datas['manage_id'] = $exist['manage_id'];
                    db('crm_customer')->where('client_id',$customer['client_id'])->update($customer_datas);
                    db('crm_customer_zhuanyi')->insert(['client_id'=>$customer['client_id'],'manage_id'=>$exist['manage_id'],'date'=>time()]);
                    $content = '从99999转给'.$exist['manage_id'];
                    self::followAddnewa($customer['client_id'], '99999', '转进客户', $content, 1, $exist['manage_id'], date("Y-m-d H:i:s",time()), 1);
                }
                // echo $exist['phone'].' '.$exist['manage_id'].' '.$exist['wx_code_name'];
                // echo "<br>";
            }
        // }
        // echo "运行完成";
    }

    public function followAddnewa($client_id, $arrange_manage_id, $follow_type, $content, $wait_deal = 0, $follow_manage_id = '', $wait_deal_time = '', $is_move = 0)
    {
        if (empty($arrange_manage_id)) {
            apiReturn(201,'安排人id不能为空');
        }
        if (empty($content)) {
            apiReturn(202,'跟进内容不能为空');
        }
        $client_ids = explode(',', $client_id);
        foreach ($client_ids as $key => $value) {
            $customer_name = db('crm_customer')->where('client_id',$value)->value('name');
            $arrange_manage_name = db('crm_manage')->where('username',$arrange_manage_id)->value('name');
            $follow_datas = array(
                'client_id'=>$value,
                'customer_name'=>$customer_name,
                'arrange_manage_id'=>$arrange_manage_id,
                'arrange_manage_name'=>$arrange_manage_name,
                'follow_type'=>$follow_type,
                'content'=>$content,
                'date'=>time()
            );
            if ($wait_deal == 1) {
                if ($is_move == 1) {
                    $follow_datas['wait_deal'] = 0;
                }else{
                    $follow_datas['wait_deal'] = 1;
                }
                $follow_datas['follow_manage_id'] = $follow_manage_id;
                $follow_manage_name = db('crm_manage')->where('username',$follow_manage_id)->value('name');
                $follow_datas['follow_manage_name'] = $follow_manage_name;
                $follow_datas['genjin_date'] = strtotime($wait_deal_time);
            }
            db('crm_customer_follow')->insert($follow_datas); 
        }
        return true;
    }

    public function noinsertCustomer()
    {
        // https://api.xuexiang.com.cn/index/test/noinsertCustomer/token/7df32de82d0a5e4d2599427eddcb4cee37db2bdb
        $list = db('crm_customer_noinsert')->where('status',0)->select();
        foreach ($list as $key => $value) {
            $customer_exist = db('crm_customer')->where('phone',$value['phone'])->find();
            if (empty($customer_exist)) {
                $max_client_id = db('crm_customer')->max('client_id');
                if (empty($max_client_id)) {
                    $client_id = 1;
                }else{
                    $client_id = $max_client_id+1;
                }
                $datas = [
                    'client_id'=>$client_id,
                    'manage_id'=>$value['manage_id'],
                    'name'=>$value['name'],
                    'phone'=>$value['phone'],
                    'source'=>$value['source'],
                    'source_type'=>$value['source_type'],
                    'grade'=>$value['grade'],
                    'weixin_nickname'=>$value['weixin_nickname'],
                    'public_paytype'=>$value['public_paytype'],
                    'date'=>$value['date']
                ];
                try {
                    db('crm_customer')->insert($datas);
                } catch (Exception $e) {
                    var_dump($key.' '.$value['phone']);
                    var_dump($e);exit;
                }
                
                $money = explode('元', $value['source'])[0];
                $order_exist = db('cooperation')->where(['number'=>$money,'phone'=>$value['phone']])->find();
                //下单的改来源
                if (!empty($order_exist)) {
                    self::setCustomerSource($value['phone'],$value['source_type'],$money);
                }
                //弹出二维码的自动转
                self::setnoInsertCustomer($value['phone']);
            }
            db('crm_customer_noinsert')->where('id',$value['id'])->update(['status'=>1]);
        }
    }

    //付款修改来源状态
    public function setCustomerSource($phone,$type,$money)
    {
        if ($money <= 69) {
            $exist = db('crm_customer')->where(array('phone'=>$phone))->find();
            if (!empty($exist)) {
                $source = db('crm_media')->alias('a')->join('tp_crm_media_next b','a.id=b.media_id')->where(['b.type'=>$type])->value('a.media');
                if (empty($source)) {
                    $datas['source'] = '39元公开课';
                }else{
                    $datas['source'] = $source;
                }
                db('crm_customer')->where('id',$exist['id'])->update($datas);
                self::customerManageAdda($phone);
                return true;
            }else{
                return false;
            }
        }else{
            $exist = db('crm_customer')->where(array('phone'=>$phone))->find();
            if (!empty($exist)) {
                if (intval($money) > 1900) {
                    if ($exist['public_paytype'] == 1) {
                        $date = intval(date("Hi",time()));
                        if ((2015 <= $date) && ($date <= 2115)) {
                            $public_paytype = 2;
                        }else{
                            $public_paytype = 3;
                        }
                        $datas['public_paytype'] = $public_paytype;
                        $datas['label'] = 1;
                        $datas['paytype_time'] = time();
                        if ($exist['source'] == '2980元公开课未付款' || $exist['source'] == '3280元公开课未付款' || $exist['source'] == '1980元公开课未付款') {
                            $source = db('crm_media')->alias('a')->join('tp_crm_media_next b','a.id=b.media_id')->where(['b.type'=>$type])->value('a.media');
                            if (empty($source)) {
                                if (intval($money) > 3000) {
                                    $datas['source'] = '3280已付款';
                                }else{
                                    $datas['source'] = '2980已付款';
                                }
                            }else{
                                $datas['source'] = $source;
                            }
                        }
                        db('crm_customer')->where(array('phone'=>$phone))->update($datas);
                        self::customerManageAdda($phone);
                    }
                }
                if ((time() - $exist['date']) <= 3000) {
                    $exist1 = db('crm_customer_repeat')->where(array('phone'=>$phone))->find();
                    if (empty($exist1)) {
                        $user = db('user')->where(['phone'=>$phone,'type'=>$type])->find();
                        if (!empty($user)) {
                            $datas1 = array(
                                'order_sn'=>'2980新增客户',
                                'realname'=>$user['realname'],
                                'phone'=>$phone,
                                'grade'=>$user['grade'],
                                'pay_type'=>1,
                                'price'=>$money,
                                'type'=>$type,
                                'typenew'=>1,
                                'date'=>time()
                            );
                            db('crm_customer_repeat')->insert($datas1);
                        }
                    }
                } 
            }
            return true;
        }
    }

    // 添加客户归属
    public function customerManageAdda($phone, $add = 0)
    {
        $customer = db('crm_customer')->where('phone',$phone)->find();
        $client_id = $customer['client_id'];

        //邀约
        if (!empty($customer['yaoyue_manage_id'])) {
            $exist = db('crm_customer_manage')->where(array('client_id'=>$client_id,'manage_id'=>$customer['yaoyue_manage_id']))->find();
            $datas = array(
                'client_id'=>$customer['client_id'],
                'manage_id'=>$customer['yaoyue_manage_id'],
                'phone'=>$customer['phone'],
                'yaoyue_status'=>$customer['yaoyue_status'],
                'keqian_status1'=>$customer['keqian_status1'],
                'keqian_status2'=>$customer['keqian_status2'],
                'label'=>$customer['label'],
                'source'=>$customer['source'],
                'grade'=>$customer['grade'],
                'public_yingqi'=>$customer['public_yingqi'],
                'public_yingqi_course1'=>$customer['public_yingqi_course1'],
                'public_yingqi_course2'=>$customer['public_yingqi_course2'],
                'formali_yingqi'=>$customer['formali_yingqi'],
                'is_refund'=>$customer['is_refund'],
                'public_paytype'=>$customer['public_paytype'],
                'source_type'=>$customer['source_type'],
                'become_zhifu'=>$customer['become_zhifu'],
                'total'=>$customer['total'],
                'yaoyue_gonghai'=>$customer['yaoyue_gonghai']
            );
            if (empty($exist)) {
                if ($add == 1) {
                    $datas['date'] = time();
                    db('crm_customer_manage')->insert($datas);
                }
            }else{
                db('crm_customer_manage')->where('id',$exist['id'])->update($datas);
            }
        }

        //教务
        if (!empty($customer['jiaowu_manage_id'])) {
            $exist = db('crm_customer_manage')->where(array('client_id'=>$client_id,'manage_id'=>$customer['jiaowu_manage_id']))->find();
            $datas = array(
                'client_id'=>$customer['client_id'],
                'manage_id'=>$customer['jiaowu_manage_id'],
                'phone'=>$customer['phone'],
                'yaoyue_status'=>$customer['yaoyue_status'],
                'keqian_status1'=>$customer['keqian_status1'],
                'keqian_status2'=>$customer['keqian_status2'],
                'label'=>$customer['label'],
                'source'=>$customer['source'],
                'grade'=>$customer['grade'],
                'public_yingqi'=>$customer['public_yingqi'],
                'public_yingqi_course1'=>$customer['public_yingqi_course1'],
                'public_yingqi_course2'=>$customer['public_yingqi_course2'],
                'formali_yingqi'=>$customer['formali_yingqi'],
                'is_refund'=>$customer['is_refund'],
                'public_paytype'=>$customer['public_paytype'],
                'source_type'=>$customer['source_type'],
                'become_zhifu'=>$customer['become_zhifu'],
                'total'=>$customer['total'],
                'yaoyue_gonghai'=>$customer['yaoyue_gonghai']
            );
            if (empty($exist)) {
                if ($add == 1) {
                    $datas['date'] = time();
                    db('crm_customer_manage')->insert($datas);
                }
            }else{
                db('crm_customer_manage')->where('id',$exist['id'])->update($datas);
            }
        }
        return true;
    }

    public function runCooperationOrder()
    {
        // https://api.xuexiang.com.cn/index/test/runCooperationOrder/token/7df32de82d0a5e4d2599427eddcb4cee37db2bdb
        $list = db('error_sn')->where(['pay'=>2,'status'=>0])->select();
        foreach ($list as $key => $value) {
            $string = explode('&out_trade_no=', $value['sn'])[1];
            $out_trade_no = substr($string, 0,10);
            $exist_cooperation = db('cooperation')->where('order_sn',$out_trade_no)->find();
            if (!empty($exist_cooperation)) {
                db('error_sn')->where('id',$value['id'])->update(['status'=>1,'order_sn'=>$out_trade_no]);
                continue;
            }
            $payInfo = db('user_pay')->field('id,user_id,order_sn,total,status,ticket,clickid,xitongnew,source_id,source_type,manage_id')->where('order_sn',$out_trade_no)->find();
            if (empty($payInfo)) {
                $payInfo = db('user_pay_new')->field('id,user_id,order_sn,total,status,ticket,clickid,xitongnew,source_id,source_type,manage_id')->where('order_sn',$out_trade_no)->order('date desc')->find();
            }
            $userInfo = db('user')->where('id',$payInfo['user_id'])->find();
            if (!empty($payInfo) && !empty($userInfo)) {
                if ($payInfo['status'] == 0) {
                    $cooperation = array(
                        'order_sn'=>$payInfo['order_sn'],
                        'user_id'=>$userInfo['id'],
                        'name'=>$userInfo['nickname'],
                        'pic'=>$userInfo['headimgurl'],
                        'realname'=>$userInfo['realname'],
                        'phone'=>$userInfo['phone'],
                        'desc'=>'记忆力支付',
                        'title'=>'超强记忆力系统班',
                        'number'=>$payInfo['total'],
                        'reply'=>'',
                        'status'=>1,
                        'date'=>time(),
                        'grade'=>$userInfo['grade'],
                        'shebei'=>$userInfo['shebei'],
                        'xitong'=>$userInfo['xitong'],
                        'liulanqi'=>$userInfo['liulanqi'],
                        'diqu'=>$userInfo['diqu'],
                        'ip'=>$userInfo['ip'],
                        'ip_area'=>$userInfo['ip_area'],
                        'phone_area'=>$userInfo['phone_area'],
                        'type'=>$userInfo['type'],
                        'pay'=>$userInfo['pay'],
                        'typenew'=>$userInfo['typenew']
                    );
                    if (isset($payInfo['ticket'])) {
                        if (!empty($payInfo['ticket'])) {
                            $cooperation['ticket'] = $payInfo['ticket'];
                        }
                    }
                    if (isset($payInfo['xitongnew'])) {
                        if (!empty($payInfo['xitongnew'])) {
                            $cooperation['xitongnew'] = $payInfo['xitongnew'];
                            $cooperation['source_id'] = $payInfo['source_id'];
                            $cooperation['source_type'] = $payInfo['source_type'];
                            $cooperation['manage_id'] = $payInfo['manage_id'];
                        }
                    }
                    $cooperation_id = db('cooperation')->insertGetId($cooperation);

                    $repeat_count = db('cooperation')->where(array('number'=>['in','0,0.1,1,3,9.9,19.9,39,69'],'phone'=>$userInfo['phone']))->count();
                    if ($repeat_count >= 2) {
                        $this->customerRepeat($payInfo['order_sn'], $userInfo['realname'], $userInfo['phone'], $userInfo['grade'], $userInfo['pay'], $payInfo['total'], $userInfo['type'], $userInfo['typenew']);
                    }
                    if (empty($cooperation_id)) {
                        echo "失败2";exit;
                    }else{
                        $datas = array('status'=>1);
                        db('user_pay')->where('id',$payInfo['id'])->update($datas);
                        //删除未支付订单
                        db('user_pay')->where(array('user_id'=>$userInfo['id'],'type'=>$userInfo['type'],'status'=>0))->delete();
                    }
                    $this->setCustomerSource($userInfo['phone'], $userInfo['type'], $payInfo['total']);
                    if (isset($payInfo['ticket'])) {
                        if (!empty($payInfo['ticket'])) {
                            $this->ticket_use($payInfo['user_id'], $userInfo['type']);
                        }
                    }
                }
            }else{
                db('error_sn')->where('id',$value['id'])->update(['status'=>1,'order_sn'=>$out_trade_no]);
            }
        }
        echo "运行完成";
    }

     /**
     * 新增重复支付客户
     */
    public function customerRepeat($order_sn, $realname, $phone, $grade, $pay_type, $price, $type, $typenew)
    {
        $exist = db('crm_customer_repeat')->where(array('order_sn'=>$order_sn,'phone'=>$phone))->find();
        if (empty($exist)) {
            $datas = array(
                'order_sn'=>$order_sn,
                'realname'=>$realname,
                'phone'=>$phone,
                'grade'=>$grade,
                'pay_type'=>$pay_type,
                'price'=>$price,
                'type'=>$type,
                'typenew'=>$typenew,
                'date'=>time()
            );
            db('crm_customer_repeat')->insert($datas);

            //加待办
            $customer = db('crm_customer')->where(array('phone'=>$phone))->find();
            if (!empty($customer)) {
                if (!empty($customer['manage_id'])) {
                    $follow_manage_name = db('crm_manage')->where('username',$customer['manage_id'])->value('name');
                    $follow_datas = array(
                        'client_id'=>$customer['client_id'],
                        'customer_name'=>$customer['name'],
                        'arrange_manage_id'=>'admin',
                        'arrange_manage_name'=>'超级管理员',
                        'follow_type'=>'重复下单',
                        'content'=>'重复下单客户',
                        'date'=>time(),
                        'wait_deal'=>1,
                        'follow_manage_id'=>$customer['manage_id'],
                        'follow_manage_name'=>$follow_manage_name,
                        'genjin_date'=>time()
                    );
                    db('crm_customer_follow')->insert($follow_datas);
                }
            }
            return 2;
        }else{
            return 1;
        }
    }

    // 使用优惠券
    public function ticket_use($user_id, $type)
    {
        $exist = db('user_ticket')->where(['user_id'=>$user_id,'type'=>$type])->find();
        if (!empty($exist)) {
            db('user_ticket')->where('id',$exist['id'])->update(['status'=>2]);
            return true;
        }else{
            return false;
        }
    }

    public function setCustomerDate()
    {
        // https://api.xuexiang.com.cn/index/test/setCustomerDate/token/7df32de82d0a5e4d2599427eddcb4cee37db2bdb
        $url = "https://xuexiang.oss-cn-hangzhou.aliyuncs.com/1692433067.xlsx";
        $data_array = excels::readExcelFile($url);
        if (empty($data_array)) {
            apiReturn(204,'文件内容为空或读取失败');
        }
        foreach ($data_array as $key => $value) {
            if ($key > 1) {
                $customer = db('crm_customer')->where('phone',$value[2])->find();
                if (!empty($customer)) {
                    // $is_empty = substr($value[12], 0,1);
                    // $is_empty1 = substr($value[12], 0,2);
                    // // var_dump($is_empty);exit;
                    // // if ($value[2] == '18908635007') {
                    // //     var_dump($value[12]);
                    // // }
                    // // if (ctype_space($is_empty)) {
                    // if (strlen(trim($is_empty)) == 0) {
                    //     $date = substr($value[12], 1);
                    //     if (strlen(trim($is_empty1)) == 0) {
                    //         $date = substr($value[12], 2);
                    //     }
                    // }else{
                    //     $date = $value[12];
                    // }
                    $date = trim($value[12]);
                    // if ($value[2] == '18908635007') {
                    //     // string(19) "2023-08-19 14:33:71"
                    //     // var_dump($date);exit;
                    //     var_dump(strtotime("2023-08-19 14:33:71"));exit;
                    // }
                    
                    db('crm_customer')->where('client_id',$customer['client_id'])->update(['date'=>strtotime($date)]);
                }
            }
        }
        echo "运行完成";
    }

    public function setCustomerIntegral()
    {
        // https://api.xuexiang.com.cn/index/test/setCustomerIntegral/token/7df32de82d0a5e4d2599427eddcb4cee37db2bdb
        $where['title'] = ['like','%开营须知%'];
        $list = db('goods_next')->where($where)->column('id');
        foreach ($list as $key => $value) {
            $count = db('crm_customer_integral')->where('course_id',$value)->delete();
            var_dump($count);
        }
    }

    public function test123321()
    {
        $phone = '15270826615';
        $customer = db('crm_customer')->where('phone',$phone)->find();
        $manage_name = db('crm_manage')->where('username',$customer['manage_id'])->value('name');
        $content = $customer['phone'].'该客户在'.$customer['manage_id'].' '.$manage_name.'库中';
        var_dump($content);
    }

    public function setCustomerDateaa()
    {
        // https://api.xuexiang.com.cn/index/test/setCustomerDateaa/token/7df32de82d0a5e4d2599427eddcb4cee37db2bdb
        $url = "https://xuexiang.oss-cn-hangzhou.aliyuncs.com/1e7b729319ffc5aa3b3f1c9482384b36.xlsx";
        $data_array = excels::readExcelFile($url);
        if (empty($data_array)) {
            apiReturn(204,'文件内容为空或读取失败');
        }
        // var_dump($data_array);exit;
        foreach ($data_array as $key => $value) {
            if ($key > 1) {
                $customer = db('crm_customer')->where('phone',$value[2])->find();
                var_dump($value[2]);exit;
                if (!empty($customer)) {
                    db('crm_customer')->where('client_id',$customer['client_id'])->update(['source'=>'3元视频号直播W']);
                }
            }
        }
        echo "运行完成";
    }

    public function setCustomerDatebb()
    {
        // https://api.xuexiang.com.cn/index/test/setCustomerDatebb/token/7df32de82d0a5e4d2599427eddcb4cee37db2bdb
        $where['date'] = array(array('egt',strtotime("2023-08-18 00:00:00")),array('elt',strtotime("2023-08-24 23:59:59")));
        $where['source'] = '和谐号直播3元H';
        $where['yaoyue_status'] = 3;
        $list = db('crm_customer')->field('client_id')->where($where)->limit(0,200)->select();
        // var_dump($list);exit;
        foreach ($list as $key => $value) {
            db('crm_customer')->where('client_id',$value['client_id'])->update(['yaoyue_status'=>4,'is_refund'=>1]);
        }
        echo "运行完成";
    }

    public function setCustomerDatecc()
    {
        // https://api.xuexiang.com.cn/index/test/setCustomerDatecc/token/7df32de82d0a5e4d2599427eddcb4cee37db2bdb
        $where['date'] = array(array('egt',strtotime("2023-08-29 00:00:00")),array('elt',strtotime("2023-08-29 23:59:59")));
        // $list = db('crm_customer')->field('phone')->where($where)->select();
        // var_dump($list);exit;3555 
        
        $where1['date'] = $where['date'];
        $list1 = db('cooperation')->field('phone')->where($where1)->select();
        $where2['date'] = $where['date'];
        $list2 = db('user_pay')->field('phone')->where($where2)->select();
        $where3['date'] = $where['date'];
        $list3 = db('user_pay_new')->field('phone')->where($where3)->select();
        $phone = [];
        foreach ($list1 as $key => $value) {
            if (!in_array($value['phone'], $phone)) {
                $phone[] = $value['phone'];
            }
        }
        foreach ($list2 as $key => $value) {
            if (!in_array($value['phone'], $phone)) {
                $phone[] = $value['phone'];
            }
        }
        foreach ($list3 as $key => $value) {
            if (!in_array($value['phone'], $phone)) {
                $phone[] = $value['phone'];
            }
        }
        var_dump($phone);exit;
    }

    public function setCustomerDatedd()
    {
        // https://api.xuexiang.com.cn/index/test/setCustomerDatedd/token/7df32de82d0a5e4d2599427eddcb4cee37db2bdb
        // $starttime = date("Y-m-d 00:00:00",time());
        // $endtime = date("Y-m-d 23:59:59",time());
        // var_dump($starttime);var_dump($endtime);exit;
        $where['date'] = array(array('egt',strtotime("2023-09-01 00:00:00")),array('elt',strtotime("2023-09-01 23:59:59")));
        $where['source'] = ['like','3元公开课未付款'];
        $list = db('crm_customer')->field('phone,source')->where($where)->select();
        var_dump($list);exit;
        foreach ($list as $key => $value) {
            if ($value['source'] == '3元公开课未付款') {
                $exist = db('cooperation')->where(['phone'=>$value['phone'],'number'=>3])->find();
                if ($exist) {
                    self::setCustomerSource($exist['phone'],$exist['type'],$exist['number']);
                    self::followAddabc($exist['phone'], $exist['type'], $exist['number'], $exist['pay'], $exist['order_sn'], $exist['typenew']);
                }
            }
        }
        echo '运行完成';
    }

    public function followAddabc($phone, $type, $money, $pay, $order_sn, $typenew = 1)
    {
        $follow_type = '付款成功';
        $arrange_manage_id = 'admin';

        $customer = db('crm_customer')->field('client_id,manage_id,name')->where('phone',$phone)->find();

        if (!empty($customer)) {
            $pay_type = ($pay == 1) ? '微信' : '支付宝';
            $typenew = ($typenew == 1) ? '公司商户' : '中心商户';
            $content = '渠道：'.$type.' | 付款方式：'.$pay_type.' | 订单号：'.$order_sn.' | 金额：'.$money.' | 收款账户：'.$typenew;
            $arrange_manage_name = db('crm_manage')->where('username',$arrange_manage_id)->value('name');
            if (empty($customer['manage_id'])) {
                return false;
            }else{
                $follow_manage_name = db('crm_manage')->where('username',$customer['manage_id'])->value('name');
            }

            $follow_datas = array(
                'client_id'=>$customer['client_id'],
                'customer_name'=>$customer['name'],
                'arrange_manage_id'=>$arrange_manage_id,
                'arrange_manage_name'=>$arrange_manage_name,
                'follow_type'=>$follow_type,
                'content'=>$content,
                'date'=>time(),
                'wait_deal'=>1,
                'follow_manage_id'=>$customer['manage_id'],
                'follow_manage_name'=>$follow_manage_name,
                'genjin_date'=>time()
            );
            db('crm_customer_follow')->insert($follow_datas);
            return true;
        }else{
            return false;
        }
    }

    public function addOrderFollow()
    {
        // https://api.xuexiang.com.cn/index/test/addOrderFollow/token/7df32de82d0a5e4d2599427eddcb4cee37db2bdb
        $id = 293804;
        $exist = db('cooperation')->where('id',$id)->find();
        if (!empty($exist)) {
            self::followAddabc($exist['phone'], $exist['type'], $exist['number'], $exist['pay'], $exist['order_sn'], $exist['typenew']);
        }
        echo "运行完成";
    }

    public function adasdas123()
    {
        // https://api.xuexiang.com.cn/index/test/adasdas123/token/7df32de82d0a5e4d2599427eddcb4cee37db2bdb
        $out_trade_no = 'hdWPKEBhr2';
        $pay_new = 0;
        $payInfo = db('user_pay')->field('id,user_id,order_sn,total,status,xitongnew,source_id,source_type,manage_id')->where('order_sn',$out_trade_no)->find();
        var_dump($payInfo);
        if (empty($payInfo)) {
            $payInfo = db('user_pay_new')->field('id,user_id,order_sn,total,status,ticket,clickid,xitongnew,source_id,source_type,manage_id')->where('order_sn',$out_trade_no)->order('date desc')->find();
            var_dump($payInfo);
            $pay_new = 1;
        }
        var_dump($pay_new);
        $datas = array('status'=>1);
        if ($pay_new == 0) {
            var_dump("111");exit;
            db('user_pay')->where('id',$payInfo['id'])->update($datas);
            //删除未支付订单
            db('user_pay')->where(array('user_id'=>$userInfo['id'],'type'=>$userInfo['type'],'status'=>0))->delete();
        }else{
            var_dump("222");exit;
            db('user_pay_new')->where('id',$payInfo['id'])->update($datas);    
        }
    }

    public function findType()
    {
        $where['date'] = array(array('egt',strtotime("2023-09-05 00:00:00")),array('elt',strtotime("2023-09-05 23:59:59")));
        $where['number'] = 19;
        $where1 = $where;
        $list = db('cooperation')->field('type')->distinct(true)->where($where)->select();
        
        foreach ($list as $key => &$value) {
            $where1['type'] = $value['type'];
            $value['number'] = db('cooperation')->where($where1)->count();
            echo 'type:'.$value['type'].' number:'.$value['number'].'<br>';
        }
    }

    public function findPhone()
    {
        $where['type'] = 32;
        $where['date'] = array(array('egt',strtotime("2023-09-05 00:00:00")),array('elt',strtotime("2023-09-05 23:59:59")));
        $where['number'] = 19;
        $list = db('cooperation')->field('type,phone')->where($where)->select();

        foreach ($list as $key => $value) {
            $where1['source_type'] = 32;
            $where1['date'] = array(array('egt',strtotime("2023-09-05 00:00:00")),array('elt',strtotime("2023-09-05 23:59:59")));
            $where1['phone'] = $value['phone'];

            $exist = db('crm_customer')->where($where1)->find();
            if ($exist) {
                unset($list[$key]);
            }
        }
        dump($list);
    }

    public function customerRepeatRecover()
    {
        // https://api.xuexiang.com.cn/index/test/customerRepeatRecover/token/7df32de82d0a5e4d2599427eddcb4cee37db2bdb
        $starttime = date("Y-m-d 00:00:00",time());
        $endtime = date("Y-m-d 23:59:59",time());
        $where['date'] = array(array('egt',strtotime($starttime)),array('elt',strtotime($endtime)));
        $where['number'] = ['in','0,0.1,1,3,9.9,19,19.9,39,69'];

        $cooperationList = db('cooperation')->field('order_sn,realname,phone,grade,number,type,pay,typenew')->where($where)->select();
        // var_dump($cooperationList);exit;

        $number = 0;
        foreach ($cooperationList as $key => $value) {
            $where1['date'] = array(array('egt',strtotime($starttime)),array('elt',strtotime($endtime)));
            $where1['phone'] = $value['phone'];
            $customerExist = db('crm_customer')->field('client_id,phone,source,source_type,date')->where($where1)->find();
            if ($customerExist) {
                continue;
            }else{
                $repeat_count = db('cooperation')->where(array('number'=>['in','0,0.1,1,3,9.9,19,19.9,39,69'],'phone'=>$value['phone']))->count();
                if ($repeat_count >= 2) {
                    $repeatReturn = $this->customerRepeat($value['order_sn'], $value['realname'], $value['phone'], $value['grade'], $value['pay'], $value['number'], $value['type'], $value['typenew']);
                    if ($repeatReturn == 2) {
                        $number++;
                    }
                }
            }
        }

        var_dump($number);exit;



        $where['source'] = ['notlike','%未付款'];

        $list = db('crm_customer')->field('client_id,phone,source,source_type')->where($where)->select();
        var_dump($list);exit;
    }

    public function findGkkOrder()
    {
        $starttime = date("Y-m-d 00:00:00",time());
        $endtime = date("Y-m-d 23:59:59",time());
        $where['date'] = array(array('egt',strtotime($starttime)),array('elt',strtotime($endtime)));
        $where['number'] = 2980;
        $list = db('cooperation')->field('FROM_UNIXTIME(date,"%Y-%m-%d %H:%i:%s") as date,grade')->where($where)->order('date DESC')->select();
        // var_dump($list);
        foreach ($list as $key => $value) {
            echo $value['date'].' '.$value['grade'].'<br>';
        }
    }

    public function setMedisNext()
    {
        // https://api.xuexiang.com.cn/index/test/setMedisNext/token/7df32de82d0a5e4d2599427eddcb4cee37db2bdb
        for ($i=1; $i < 100; $i++) { 
            db('crm_media_next')->where('type',$i)->update(['media_id'=>73]);
        }
        
    }

    public function setCustomerFollowType()
    {
        //https://api.xuexiang.com.cn/index/test/setCustomerFollowType/token/7df32de82d0a5e4d2599427eddcb4cee37db2bdb

        db('crm_customer_follow')->where(['follow_type'=>'通话回访/视频带练'])->update(['follow_type'=>'普通通话回访']);

        echo "成功";
    }

    public function setCache()
    {
        //https://api.xuexiang.com.cn/index/test/setCache/token/7df32de82d0a5e4d2599427eddcb4cee37db2bdb
        $where['type'] =1;
        $where['status'] = 1;
        $phone =(new GoodsPhone())->where('course_id','in',[190,191])->group('phone')->limit(0,1)->column('phone');
        
        // var_dump($sql);exit;
        // var_dump(count($phone));exit;
        // dump(count($phone).'手机号');
        $number = 0;
        $number1 = 0;
        $number2 = 0;
        foreach ($phone as $value){
            $id = db('user_course')->where('phone',$value)->value('id');
            if (empty($id)) {
                $number++;
                echo '无账号：'.$value;
            }
            $yingqi =(new GoodsPhone)->where('phone',$value)->group('course_id')->column('course_id');
            $sql = (new GoodsPhone())->getLastSql();
            var_dump($sql);exit;
            if(empty($yingqi)){
                $number1++;
                echo '无营期：'.$value;
            }
            $goods=(new Goods())->where('id','in',$yingqi)->where($where)->field('id,title,desc,pic1,name,teacher,status,date,link')->with('time')->limit(1)->page(1)->order('id desc')->select();
            if (empty($goods)) {
                $number2++;
            }
            // $user = UserServer::get_user_goods($id . '1');
            // if(empty($user)){
            //     if(count($goods)>0) {
            //         UserServer::set_user_goods($id . '1', $goods);
            //     }
            // }
        }
        // echo "无账号：".$number;
        // echo "无营期：".$number1;
        // echo "无课程：".$number2;
        Db::listen(function($sql, $time, $explain){
            echo $sql.'<br>运行时间：['.$time.'s]';
            var_dump($explain);
        });
    }

    public function setCustomerYaoyueStatus()
    {
        //https://api.xuexiang.com.cn/index/test/setCustomerYaoyueStatus/token/7df32de82d0a5e4d2599427eddcb4cee37db2bdb
        // $starttime = date("2023-09-01 00:00:00",time());
        // $endtime = date("2023-09-07 23:59:59",time());
        // $where['date'] = array(array('egt',strtotime($starttime)),array('elt',strtotime($endtime)));

        $where['course_id'] = ['in','694'];
        db('crm_durations')->where($where)->delete();
        db('crm_course_duration')->where($where)->delete();
        // var_dump($a);var_dump($b);exit;
    }

    public function aaa1546()
    {
        $starttime = date("2023-09-18 00:00:00",time());
        $endtime = date("2023-09-18 23:59:59",time());
        $where['date'] = array(array('egt',strtotime($starttime)),array('elt',strtotime($endtime)));
        $where['type'] = ['between','720,749'];
        $list = db('cooperation')->where($where)->count();
        var_dump($list);exit;
    }

    public function aaa15463()
    {
        $starttime = date("2023-09-25 00:00:00",time());
        $endtime = date("2023-09-25 23:59:59",time());
        $where['date'] = array(array('egt',strtotime($starttime)),array('elt',strtotime($endtime)));
        $where['number'] = 3;
        $phones = db('cooperation')->where($where)->column('phone');


        $where1['date'] = array(array('egt',strtotime($starttime)),array('elt',strtotime($endtime)));
        $where1['source'] = '3元公开课H';
        $list1 = db('crm_customer')->where($where1)->column('phone');

        $a = [];
        foreach ($phones as $key => $value) {
            if (!in_array($value, $list1)) {
                $a[] = $value;
            }
        }

    
        var_dump($a);exit;
    }

    public function aaa1546324()
    {
        // https://api.xuexiang.com.cn/index/test/aaa1546324/token/7df32de82d0a5e4d2599427eddcb4cee37db2bdb
        $where['type'] = ['between',[1,100]];
        $list = db('crm_media_next')->field('id,type')->where($where)->order('type asc')->select();
        foreach ($list as $key => $value) {
            db('crm_media_next')->where('id',$value['id'])->update(['media_id'=>7]);
        }
    }

    public function aaa15463245()
    {
        // https://api.xuexiang.com.cn/index/test/aaa15463245/token/7df32de82d0a5e4d2599427eddcb4cee37db2bdb
        $starttime = date("2023-09-26 00:00:00",time());
        $endtime = date("2023-09-26 23:59:59",time());
        $where['date'] = array(array('egt',strtotime($starttime)),array('elt',strtotime($endtime)));
        $where['source_type'] = ['in','515,516'];
        $list = db('crm_customer')->field('client_id,source,source_type')->where($where)->select();
        foreach ($list as $key => $value) {
            if ($value['source'] == '和谐号直播3元H') {
                db('crm_customer')->where('client_id',$value['client_id'])->update(['source'=>'3元公开课H']);
            }
        }
    }

    public function aaa154632456()
    {
        // https://api.xuexiang.com.cn/index/test/aaa154632456/token/7df32de82d0a5e4d2599427eddcb4cee37db2bdb
        $list = db('crm_yingqi_count')->select();
        foreach ($list as $key => $value) {
            $grade = db('crm_customer')->where('phone',$value['phone'])->value('grade');
            db('crm_yingqi_count')->where('id',$value['id'])->update(['grade'=>$grade]);
        }
    }

    public function aaa12345689()
    {
        // https://api.xuexiang.com.cn/index/test/aaa12345689/token/7df32de82d0a5e4d2599427eddcb4cee37db2bdb
        $url = "https://xuexiang.oss-cn-hangzhou.aliyuncs.com/7737c318c2c0ae3ad856f36cfd3a366c.xlsx";
        $data_array = excels::readExcelFile($url);
        if (empty($data_array)) {
            apiReturn(204,'文件内容为空或读取失败');
        }
        // var_dump($data_array);exit;
        $phones1 = [];
        $phones2 = [];
        foreach ($data_array as $key => $value) {
            $customer = db('crm_customer')->field('client_id,manage_id')->where('phone',$value[0])->find();
            if ($customer) {
                if ($customer['manage_id'] == 10018) {
                    self::zhuanyia($value[1],$customer['client_id']);
                }else{
                    db('crm_customer')->where('client_id',$customer['client_id'])->update(['yaoyue_status'=>1]);
                    $phones1[] = $value[0];
                }
            }else{
                $phones2[] = $value[0];
            }
        }
        echo "不在系统中：".implode(',', $phones2);
        echo "不在陈晶名下：".implode(',', $phones1);
        echo "运行完成";
    }

    public function zhuanyia($manage_id,$client_id)
    {
        $value = $client_id;
        $manage_type = db('crm_manage')->where('username',$manage_id)->value('type');
        if (empty($manage_type)) {
            apiReturn(201,'转给工号不存在');
        }
        $customer_manage_id = db('crm_customer')->where('client_id',$value)->value('manage_id');
        $customer_datas['manage_id'] = $manage_id;
        
        $customer_datas['jiaowu_manage_id'] = $manage_id;
        $customer_datas['jiaowu_zhuanyi_date'] = time();
        //添加邀约老师到客户信息
        $where['jiaowus'] = array('like','%'.$manage_id.'%');
        $yaoyue_manage_id = db('crm_manage')->where($where)->value('username');
        if (!empty($yaoyue_manage_id)) {
            $customer_datas['yaoyue_manage_id'] = $yaoyue_manage_id;
            $customer_datas['yaoyue_zhuanyi_date'] = time();
        }else{
            $customer_datas['yaoyue_manage_id'] = '';
        }
        $customer_datas['yaoyue_status'] = 1;
        $customer_datas['zhuanyi_date'] = time();

        db('crm_customer')->where('client_id',$value)->update($customer_datas);

        $content = '从'. $customer_manage_id.'转给'.$manage_id;
        $this->followAdd($value, '10000', '转进客户', $content, 1, $manage_id, date("Y-m-d H:i:s",time()), 1);
    }

    public function aaa1546324566()
    {
        // https://api.xuexiang.com.cn/index/test/aaa1546324566/token/7df32de82d0a5e4d2599427eddcb4cee37db2bdb
        $starttime = date("2023-10-18 00:00:00",time());
        $endtime = date("2023-10-18 23:59:59",time());
        $where['date'] = array(array('egt',strtotime($starttime)),array('elt',strtotime($endtime)));
        $typeString = [];
        for ($i=101; $i <= 199; $i++) { 
            $typeString[] = $i;
        }
        $where['source_type'] = ['in',implode(',', $typeString)];
        $list = db('crm_customer')->field('client_id,source,source_type')->where($where)->select();
        // var_dump($list);exit;
        foreach ($list as $key => $value) {
            if ($value['source'] != '39元公开课未付款') {
                db('crm_customer')->where('client_id',$value['client_id'])->update(['source'=>'39元公开课']);
            }
        }
    }

    public function aaa15463245667()
    {
        // https://api.xuexiang.com.cn/index/test/aaa15463245667/token/7df32de82d0a5e4d2599427eddcb4cee37db2bdb
        $where['manage_id'] = "11006";
        $where['public_yingqi'] = ['in','219,215'];
        $list = db('crm_customer')->field('phone')->where($where)->select();
        // var_dump($list);exit;
        foreach ($list as $key => $value) {
            $where1['course_id'] = ['in','125,126,67,78'];
            $where1['phone'] = $value['phone'];
            $a = db('goods_phone')->where($where1)->select();
            if ($a) {
                db('goods_phone')->where($where1)->delete();
            }
            
        }
        echo "完成";
    }

    public function aaa154632456677()
    {
        // https://api.xuexiang.com.cn/index/test/aaa154632456677/token/7df32de82d0a5e4d2599427eddcb4cee37db2bdb

        $starttime = "2023-10-13 00:00:00";
        $endtime = "2023-10-26 23:59:59";
        $where['date'] = array(array('egt',strtotime($starttime)),array('elt',strtotime($endtime)));
        $where['source'] = "0元领取";
        $where['yaoyue_status'] = 0;
        $list = db('crm_customer')->field('phone')->where($where)->select();
        var_dump($list);exit;
        foreach ($list as $key => $value) {
            db('crm_customer')->where('phone',$value['phone'])->update(['yaoyue_status'=>3]);
        }
        echo "完成";
    }

    public function lizhizhuanyi()
    {
        // https://api.xuexiang.com.cn/index/test/lizhizhuanyi/token/7df32de82d0a5e4d2599427eddcb4cee37db2bdb
        $where['manage_id'] = ['in','34100,34200,34207,34102,34107,34204,34101,34103,34202,34205,35111'];
        // $list = db('crm_customer')->where($where)->count();
        $list = db('crm_customer')->where($where)->update(['manage_id'=>'1999']);
        // var_dump($list);exit;
        echo "完成";
    }

    public function ceshi121323()
    {
        // https://api.xuexiang.com.cn/index/test/ceshi121323/token/7df32de82d0a5e4d2599427eddcb4cee37db2bdb
        $time = array(
            'starttime'=>date('Y-m-d 00:00:00',strtotime("-360 day")),
            'endtime'=>date('Y-m-d')." 23:59:59"
        );
        var_dump($time);exit;



        if (!empty($exist)) {
            //待办
            $customer = db('crm_customer')->where('phone',$data['phone'])->find();



            $this->apiReturn1(210,"已付款");
        }





    }
}
